package com.wawayaya.ui.widget;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.wawayaya.bean.BookShelf;
import com.wawayaya.bean.HomePage;
import com.wawayaya.ui.ShelfMoreActivity;
import com.wawayaya.ui.bookcover.BookCoverAdapter;
import com.wawayaya.util.ImageUtils;
import com.wawayaya.util.TLog;
import com.wawayaya.util.event.ShelfEvent;

import java.util.HashMap;
import wawayaya.kids_iread.R;

/**
 * 图书 书架 当前 行布局,根据数据动态生成
 * 分为两种类型
 * 1. 显示 图书   最多 5个item
 * 2. 显示广告栏  最多 3 个item
 */
public class BookRack {

	private Context context = null;

	private View m_v = null;
	private ImageView m_ivcatalogimg = null;
	private TextView m_tvRackname = null;

	private LinearLayout rlMoreBook = null;
	private ImageView m_bntomorelist = null;
	private TextView  tvMore = null;

	private RecyclerView recyclerView = null;
    private BookCoverAdapter bookCoverAdapter;

    private  BookShelf  bookShelf;
	private  HashMap<String, Integer> catalogIcon = null;
	private  HashMap<String, Integer> moreIcon = null;
    private  boolean   isZh;

	public BookRack(
			Context context,
			View v,
			final HomePage homePage,
			boolean isZh,
			HashMap<String, Integer> catalogIcon,
			HashMap<String, Integer> moreIcon) {
		this.m_v = v;
		this.catalogIcon = catalogIcon;
		this.moreIcon = moreIcon;
        this.context = context;
        this.isZh = isZh;
		this.bookShelf = homePage.getBookShelf();
		this.initView();
        this.setListener();
        this.loadData();
	}

	private void initView(){
		m_ivcatalogimg = (ImageView)m_v.findViewById(R.id.iv_catalogimg);
		m_ivcatalogimg.setBackgroundResource(getCatalogIconId(bookShelf.getBsFontColor()));

		m_tvRackname = (TextView)m_v.findViewById(R.id.tv_rackname);
		TLog.log("BOOKNAME  en:" + bookShelf.getBsNameEn() + "  cn:" + bookShelf.getBsNameZh());
		m_tvRackname.setText(isZh ? bookShelf.getBsNameZh() : bookShelf.getBsNameEn());
		m_tvRackname.setTextColor(ImageUtils.hashmarkcolorstr2colorint(bookShelf.getBsFontColor(), Color.BLACK));

		m_bntomorelist = (ImageView)m_v.findViewById(R.id.iv_tomore_list);
		m_bntomorelist.setBackgroundResource(getMoreIconId(bookShelf.getBsFontColor()));
		rlMoreBook = (LinearLayout)m_v.findViewById(R.id.ll_more_book);

        tvMore = (TextView)m_v.findViewById(R.id.tv_more_text);
		tvMore.setTextColor(ImageUtils.hashmarkcolorstr2colorint(bookShelf.getBsFontColor(), Color.BLACK));

		recyclerView = (RecyclerView)m_v.findViewById(R.id.rv_bookrack);

		GridLayoutManager gridLayoutManager=new GridLayoutManager(context,5);

		recyclerView.setLayoutManager(gridLayoutManager);

		recyclerView.setHasFixedSize(true);

		bookCoverAdapter = new BookCoverAdapter(context,isZh,bookShelf.getBooksList());
		recyclerView.setAdapter(bookCoverAdapter);
	}

	public View getView() {
		return m_v;
	}


	private void  setListener(){
		rlMoreBook.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent  intent =new Intent(context,ShelfMoreActivity.class);
				 context.startActivity(intent);
			}
		});

	}

	private void  loadData(){

	}

    private int getCatalogIconId(String  scolorname){
		if(null != catalogIcon){
			Integer ii = catalogIcon.get(scolorname);
			if(null != ii) {
				return ii.intValue();
			}
		}
		return R.drawable.icon_flower_red;
	}

	private int getMoreIconId(String scolorname){
		if(null != moreIcon){
			Integer ii = moreIcon.get(scolorname);
			if(null != ii) {
				return ii.intValue();
			}
		}
		return R.drawable.more_arrow_red;
	}
	
}
